package org.jeecgframework.boot.springaimcpservice.service;


import ch.qos.logback.core.util.StringUtil;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import jakarta.annotation.Resource;
import org.jeecgframework.boot.springaimcpservice.common.Result;
import org.jeecgframework.boot.springaimcpservice.entity.*;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ExpertPersonToolService {

    @Resource
    IExpertPersonService expertPersonService;


    @Resource
    private IShieldParameterService shieldParameterService;

    @Resource
    IDunGouInfoService dunGouInfoService;

    @Resource
    IShieldService shieldService;

    @Resource
    IOpinionManagementService opinionManagementService;
    //专家
    @Tool(description = "获取盾构专家列表信息")
    public Result<List<ExpertPerson>> getExpertList()
    {
        List<ExpertPerson> list = expertPersonService.list().subList(0, 5);
        return Result.ok(list);
    }
    //盾构机---------------------------------------------------------------------------------------------------------------------------------------------
    @Tool(description = "设备厂商与型号总结类问题：1、单一模式和复合模式的盾构机分别有多少台 2、海瑞克、中铁装备、铁建重工、三菱重工四家厂商的盾构机应用数量占比 3、盾构机基本信息(品牌、型号、设备类型等)分析查询 4、盾构机再制造（旧机改造）项目主要选择的厂商合作 5、泥水盾构数量统计 6、土压盾构数量统计 7、泥水/土压双模盾构机数量统计")
    public Result<List<Shield>> getShields()
    {
        List<Shield> list = shieldService.list();
        return Result.ok(list);
    }

    @Tool(description = "在我们的案例中，直径超过10米的超大直径盾构机，主要由哪些厂商提供？请列出厂商和对应的具体项目名称")
    public Result<Map<String, Object>> diameterGreaterThan(){
        Map<String, Object> resultMap = new HashMap<>();
        List<ShieldVo> Shieldlit= shieldService.diameterGreaterThan();
        if (Shieldlit.size()>0){
            for (ShieldVo shieldVo : Shieldlit) {
                resultMap.put(shieldVo.getBrand(),shieldVo.getObjectName());
            }
        }

        return Result.OK(resultMap);
    }
    @Tool(description = "请列出所有应用了双模式盾构（如EPB/SPB双模、EPB/TBM双模）的案例项目名称及其设备厂商")
    public Result<Map<String, Object>> DualModeShieldTunneling(){
        Map<String, Object> resultMap = new HashMap<>();
        List<ShieldVo> Shieldlit= shieldService.DualModeShieldTunneling();
        if (Shieldlit.size()>0){
            for (ShieldVo shieldVo : Shieldlit) {
                resultMap.put(shieldVo.getObjectName(),shieldVo.getBrand());
            }
        }
        return Result.OK(resultMap);
    }

    @Tool(description = "针对城市地铁区间（6m-7m直径）的土压平衡盾构，案例中最常出现的是哪几个厂商的设备")
    public Result<Map<String, Object>> manufacturer(){
        Map<String, Object> resultMap = new HashMap<>();
        List<ShieldVo> Shieldlit= shieldService.manufacturer();
        if (Shieldlit.size()>0){
            for (ShieldVo shieldVo : Shieldlit) {
                resultMap.put(shieldVo.getBrand(),shieldVo.getBrand());
            }
        }
        return Result.OK(resultMap);
    }


    //盾构参数----------------------------------------------------------------------------------------------------------------------------------
    @Tool(description = "总结类问题：1、盾构机参数表信息 2、案例中现有盾构机刀盘类型及尺寸总结 3、主轴承直径为2.6m和3.0m的盾构机数量统计 4、开挖直径在6m-7m之间，主驱动为液压驱动的盾构机数量 5、土压盾构机皮带角度最大度数及9°（含）以下的盾构机数量 6、盾构机刀盘开口率及根据地层的变化情况")
    public Result<List<ShieldParameter>> getShieldParameter( String  Parameter)
    {
        QueryWrapper<ShieldParameter> ShieldParameterWrapper=new QueryWrapper<>();
        if(Parameter.contains("主轴承直径")){
            ShieldParameterWrapper.like("detail_part_name",Parameter);
        }else
        if(Parameter.contains("开挖直径")){
            ShieldParameterWrapper.like("detail_part_name",Parameter);
        }else {
            ShieldParameterWrapper.like("detail_part_name",Parameter);
        }


        List<ShieldParameter> list = shieldParameterService.list(ShieldParameterWrapper);
        return Result.ok(list);
    }

    @Tool(description = "地层与设备适配性问题：1、针对磨损性极强的地层时，选用的盾构机刀盘和刀具设计有什么不同 2、案例中现有盾构机刀盘类型及尺寸总结 3、刀盘额定扭矩超过5.5MNm的盾构机数量 4、不同刀盘直径(6280mm、6680mm、8840mm)的盾构机数量占比")
    public Result<List<ShieldParameter>> getShieldParameterbydp()
    {
        QueryWrapper<ShieldParameter> expertPersonQueryChainWrapper=new QueryWrapper<>();
        expertPersonQueryChainWrapper.eq("main_part_name","刀盘");
        List<ShieldParameter> list = shieldParameterService.list(expertPersonQueryChainWrapper);
        return Result.ok(list);
    }




    @Tool(description = "在小半径曲线（R<250m）掘进的案例中，现有案例采用了哪些厂家的什么类型的盾构机？")
    public Result<Map<String, Object>> manufacturerType() {
        Map<String, Object> resultMap = new HashMap<>();
        List<ShieldVo> Shieldlit = shieldService.manufacturerType();
        if (Shieldlit.size() > 0) {
            for (ShieldVo shieldVo : Shieldlit) {
                resultMap.put(shieldVo.getBrand(), shieldVo.getShieldType());
            }
        }
        return Result.OK(resultMap);
    }


    @Tool(description = "请总结一下，各主要厂商的盾构机在关键部件（如主驱动、密封系统）的常用配置上有何不同")
    public Result<Map<String, Object>> ShieldconfigurationDifferent(){
        Map<String, Object> resultMap = new HashMap<>();
        List<ShieldParameterItem> list=shieldParameterService.ShieldconfigurationDifferent();
        if (list.size()>0){
            for (ShieldParameterItem shieldParameterItem : list){
                resultMap.put(shieldParameterItem.getBrand(),shieldParameterItem.getMainPartName()+"->"+shieldParameterItem.getDetailPartName()+":"+shieldParameterItem.getParameter());
            }
        }

        return Result.OK(resultMap);
    }
    @Tool(description = "请列出所有由欧洲厂商（如海瑞克、维尔特）提供设备的案例项目及其主要地层类型")
    public Result<Map<String, Object>> FromEurope(){
        Map<String, Object> resultMap = new HashMap<>();
        List<ShieldVo> Shieldlit= shieldService.FromEurope();
        if (Shieldlit.size()>0){
            for (ShieldVo shieldVo : Shieldlit) {
                resultMap.put("项目",shieldVo.getObjectName());
            }
        }

        return Result.OK(resultMap);
    }

//专家意见--------------------------------------------------------------------------------------------------------------------------------------------------------------------


    @Tool(description = "地层与设备适配性问题：1、专家意见分析 2、专家意见中出现\"刀盘强度\"或\"刀盘刚度\"的方案数量统计 3、专家意见中含有\"泥饼\"字样的方案数量统计 ")
    public Result<List<OpinionManagement>> getOpinionManagement()
    {
        List<OpinionManagement> list = opinionManagementService.list();
        return Result.ok(list);
    }




///------------------------------------------------

    @Tool(description = "根据编号或者姓名获取盾构专家人员信息")
    public Result<ExpertPerson> getExperInfo(String expertId,String expertName)
    {
        LambdaQueryWrapper<ExpertPerson> expertPersonQueryChainWrapper=new LambdaQueryWrapper<>();
        if (!StringUtil.isNullOrEmpty(expertId))
        {
            expertPersonQueryChainWrapper.eq(ExpertPerson::getId,expertId);
        }
        if (!StringUtil.isNullOrEmpty(expertName))
        {
            expertPersonQueryChainWrapper.eq(ExpertPerson::getName,expertName);
        }
        ExpertPerson expertPerson = expertPersonService.list(expertPersonQueryChainWrapper).stream().findFirst().orElse(new ExpertPerson());
        return Result.ok(expertPerson);
    }

    public Result<List<Map<String, Object>>> getDunGouInfo() throws Exception {
        List<Map<String, Object>> dunGouInfo = dunGouInfoService.getDunGouInfo();
        return Result.ok(dunGouInfo);
    }
    @Tool(description = "生成或导出专家信息")
    public Result<String> exportExpertPerson() throws Exception {
       String fileName = "D:/dosoft/upload/expert.xlsx";
        List<ExpertPerson> list = expertPersonService.list().subList(5, 10);
        List<ExpertPersonVo> expertPersonVoList = new ArrayList<>();
        for (ExpertPerson expertPerson : list) {
            ExpertPersonVo expertPersonVo = new ExpertPersonVo();
            expertPersonVo.setName(expertPerson.getName());
            expertPersonVo.setPosition(expertPerson.getPosition());
            expertPersonVo.setPhoneNumber(expertPerson.getUnitName());
            expertPersonVo.setSex(expertPerson.getSex());
            expertPersonVo.setTitle(expertPerson.getTitle());
            expertPersonVoList.add(expertPersonVo);
        }
       FileOutputStream outputStream = new FileOutputStream(new File(fileName));
       try {
           EasyExcel.write(outputStream, ExpertPersonVo.class).sheet("专家信息").doWrite(expertPersonVoList);
       }
       catch (Exception exception)
       {
           throw  new Exception("生成或导出专家信息失败");
       }

        String result = "http://127.0.0.1:8080/file/expert.xlsx";
       return Result.ok(result);
    }


}
